package com.xjh.him.adapter;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;

import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.xjh.him.R;
import com.xjh.him.dto.friend.FriendDTO;
import com.xjh.him.utils.Utils;
import com.xuexiang.xui.utils.DrawableUtils;
import com.xuexiang.xui.widget.textview.supertextview.SuperTextView;
import com.xuexiang.xutil.display.DensityUtils;

import org.xjh.tool.Checks;

import java.util.List;
import java.util.Map;

public class FriendAdapter extends BaseExpandableListAdapter {
    private List<String> characterList;
    private Map<String, List<FriendDTO>> map;
    private Activity mActivity;

    private ExpandableListView.OnChildClickListener childClickListener;
    private ExpandableListView.OnGroupClickListener groupClickListener;


    public FriendAdapter(Activity activity, List<String> characterList,
                         Map<String, List<FriendDTO>> map) {
        this.mActivity = activity;
        this.characterList = characterList;
        this.map = map;
    }


    @Override
    public int getGroupCount() {
        return characterList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return map.get(characterList.get(groupPosition)).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return characterList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return map.get(characterList.get(groupPosition)).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View view, ViewGroup parent) {
        FriendAdapter.GroupViewHolder groupViewHolder;
        if (view == null) {
            view = LayoutInflater.from(mActivity).inflate(R.layout.adapter_group_item, parent, false);
            groupViewHolder = new FriendAdapter.GroupViewHolder();
            groupViewHolder.stvListGroup = view.findViewById(R.id.stv_list_group);
            view.setTag(groupViewHolder);
        }else {
            groupViewHolder = (FriendAdapter.GroupViewHolder) view.getTag();
        }
        if (isExpanded) {
            groupViewHolder.getStvListGroup().setLeftIcon(R.drawable.ic_arrow_down);
        }else {
            groupViewHolder.getStvListGroup().setLeftIcon(R.drawable.ic_arrow_right);
        }
        groupViewHolder.getStvListGroup().setLeftString(characterList.get(groupPosition));

        View finalView = view;
        groupViewHolder.getStvListGroup().setOnSuperTextViewClickListener(new SuperTextView.OnSuperTextViewClickListener() {
            @Override
            public void onClick(SuperTextView superTextView) {
                groupClickListener.onGroupClick(
                        mActivity.findViewById(R.id.elv_friend_list),
                        finalView, groupPosition, getGroupId(groupPosition));
            }
        });
        return view;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent) {
        FriendAdapter.ChildViewHolder childViewHolder;
        if (view == null) {
            view = LayoutInflater.from(mActivity).inflate(R.layout.adapter_friend_item, parent, false);
            childViewHolder = new FriendAdapter.ChildViewHolder();
            childViewHolder.stvListItem = view.findViewById(R.id.stv_list_item);
            view.setTag(childViewHolder);
        }else {
            childViewHolder = (FriendAdapter.ChildViewHolder) view.getTag();
        }
        SuperTextView stvListItem = childViewHolder.getStvListItem();
        FriendDTO friendDTO = (FriendDTO) getChild(groupPosition, childPosition);
        if (Checks.isEmpty(friendDTO.getAvatarUrl())) {
            BitmapDrawable bitmapDrawable = DrawableUtils.createCircleDrawableWithText(
                    mActivity.getResources(),
                    DensityUtils.dip2px(38),
                    ContextCompat.getColor(mActivity, R.color.app_color_theme_7),
                    friendDTO.getNickname().substring(0, 1),
                    com.xuexiang.xui.utils.DensityUtils.sp2px(mActivity, 20),
                    Color.WHITE);
            stvListItem.setLeftIcon(bitmapDrawable);
        }else {
            ImageView leftIconIV = stvListItem.getLeftIconIV();
            Utils.setImageViewParams(leftIconIV, 40, 40);
            Glide.with(mActivity)
                    .load(friendDTO.getAvatarUrl())
                    .into(leftIconIV);
        }
        stvListItem.setLeftTopString(friendDTO.getNickname());
        stvListItem.setLeftBottomString(friendDTO.getSignature());

        View finalView = view;
        stvListItem.setOnSuperTextViewClickListener(new SuperTextView.OnSuperTextViewClickListener() {
            @Override
            public void onClick(SuperTextView superTextView) {
                childClickListener.onChildClick(
                        mActivity.findViewById(R.id.elv_friend_list),
                        finalView,
                        groupPosition,
                        childPosition,
                        getChildId(groupPosition, childPosition));
            }
        });
        return view;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public void setChildClickListener(ExpandableListView.OnChildClickListener childClickListener) {
        this.childClickListener = childClickListener;
    }

    public void setGroupClickListener(ExpandableListView.OnGroupClickListener groupClickListener) {
        this.groupClickListener = groupClickListener;
    }

    private static class GroupViewHolder {
        public SuperTextView stvListGroup;

        public SuperTextView getStvListGroup() {
            return stvListGroup;
        }

        public void setStvListGroup(SuperTextView stvListGroup) {
            this.stvListGroup = stvListGroup;
        }
    }

    private static class ChildViewHolder {
        public SuperTextView stvListItem;

        public SuperTextView getStvListItem() {
            return stvListItem;
        }

        public void setStvListItem(SuperTextView stvListItem) {
            this.stvListItem = stvListItem;
        }
    }
}
